﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using Newtonsoft.Json;
using System.Collections;

namespace Read.Utility
{
    public static class Extensions
    {
        /// <summary>
        /// 判断字符串是否能转换成有效的Int32格式
        /// </summary>
        public static bool IsInt32(this string str)
        {
            int i;
            return int.TryParse(str, out i);
        }

        /// <summary>
        /// 判断字符串是否能转换成有效的Int64格式
        /// </summary>
        public static bool IsInt64(this string str)
        {
            long i;
            return long.TryParse(str, out i);
        }

        /// <summary>
        /// 将指定的字符串转换成布尔值
        /// </summary>
        public static bool ToBoolean(this string str)
        {
            return Convert.ToBoolean(str);
        }

        /// <summary>
        /// 将指定的字符串转换成Int32
        /// </summary>
        public static int ToInt32(this string str)
        {
            return int.Parse(str);
        }

        /// <summary>
        /// 将指定的字符串转换成Int64
        /// </summary>
        public static long ToInt64(this string str)
        {
            return long.Parse(str);
        }

        /// <summary>
        /// 将数字的指定字符串表示形式转换为等效的十进制数。
        /// </summary>
        public static decimal ToDecimal(this string str)
        {
            return decimal.Parse(str);
        }

        /// <summary>
        /// 按照指定的长度来截断字符串
        /// </summary>
        public static string CutOff(this string str, int length)
        {
            return (str.Length <= length) ? str : str.Substring(0, length) + "...";
        }

        /// <summary>
        /// 字符串转MD5
        /// </summary>
        public static String ToMD5(this String text)
        {
            Encoding encoding = Encoding.UTF8;
            byte[] result = encoding.GetBytes(text);
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] output = md5.ComputeHash(result);
            var ret = BitConverter.ToString(output).Replace("-", "").ToLower();
            return ret;
        }

        /// <summary>
        /// IEnumerable<T>转换成Json字符串
        /// </summary>
        public static String ToJson<T>(this IEnumerable<T> list)
        {
            return JsonConvert.SerializeObject(list);
        }

        /// <summary>
        /// 对象转换成Json字符串
        /// </summary>
        public static String ToJson(this Object model)
        {
            return JsonConvert.SerializeObject(model);
        }

        /// <summary>
        /// 获取包含1-9键值的HashTable
        /// </summary>
        public static Dictionary<int, string> NumberToChinese()
        {
            var dic = new Dictionary<int, string>();
            dic.Add(1, "一");
            dic.Add(2, "二");
            dic.Add(3, "三");
            dic.Add(4, "四");
            dic.Add(5, "五");
            dic.Add(6, "六");
            dic.Add(7, "七");
            dic.Add(8, "八");
            dic.Add(9, "九");
            return dic;
        }
    }
}
